/*
 * Created by JFormDesigner on Thu Jun 27 08:12:51 CST 2024
 */

package scct.chenhui.ui;

import java.awt.event.*;
import scct.chenhui.dao.UserDao;
import scct.chenhui.model.User;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author chen
 */
public class PanelUserManager extends JPanel {
    DefaultTableModel model;
    FormMain main;
    public PanelUserManager(FormMain main) {
        this.main=main;

        initComponents();
        initView();//自定义的初始化方法
    }
    private void initView() {
//        自己的初始化
//        获取表格对象的数据模型，通过对模型的操作，可以为表格添加和现实数据
       model = (DefaultTableModel) tabUser.getModel();
       model.addColumn("id");
       model.addColumn("用户名");
       model.addColumn("密码");
       model.addColumn("性别");
       model.addColumn("年龄");
       model.addColumn("手机号码");
        getTable();
    }

//    读取数据库的用户表，显示在table中
    private  void  getTable()
    {
//        清除表格原有数据
        model.setRowCount(0);
//        读取全部用户
        UserDao userDao = new UserDao();
        List<User> allUser = userDao.getAllUser();

        for (User user : allUser) {
//            么个用户添加到表格一行
            model.addRow(new Object[]{user.getId(),user.getUsername(),user.getPassword(),
            user.getGender(),user.getAge(),user.getMobile()});
        }

    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgUserAdd dlgUserAdd = new DlgUserAdd(main);
//        设置为模态对话框
        dlgUserAdd.setModal(true);
        dlgUserAdd.setVisible(true);
        //此时就关闭了对话框，重新读取数据库
        getTable();
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();

        //======== this ========
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
